﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace V2TEK.NET.SH.Silverlight.Charts.GanttHelper
{
    public class SimpleTaskRenderer : AbstractTaskEntryRenderer
    {
        public SimpleTaskRenderer()
        {
            this.DefaultStyleKey = typeof(SimpleTaskRenderer);

            //base.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(SimpleTaskRenderer_PropertyChanged);
        }

        //void SimpleTaskRenderer_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        //{
        //    switch (e.PropertyName)
        //    {
        //        case "Data":
        //            if (Data != null)
        //            {
        //                Data.PropertyChanged -= Data_PropertyChanged;
        //                Data.PropertyChanged += Data_PropertyChanged;
        //            }
        //            break;
        //    }
        //}

        //void Data_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        //{
        //    switch (e.PropertyName)
        //    {
        //        case AbstractTaskDescriptor.COMPLETE_PERCENT_PROPERTY:
        //            UpdatePercent();
        //            break;
        //    }
        //}

        ///// <summary>
        ///// 更新百分比
        ///// </summary>
        //public void UpdatePercent()
        //{
        //    if (mCompletedCol != null && mUnCompletedCol != null)
        //    {
        //        mCompletedCol.Width = new GridLength(Data.PercentComplete * 0.01, GridUnitType.Star);
        //        mUnCompletedCol.Width = new GridLength(1 - Data.PercentComplete * 0.01, GridUnitType.Star);
        //    }
        //}

        protected ColumnDefinition mCompletedCol;
        protected ColumnDefinition mUnCompletedCol;
        protected Grid mTaskBar;

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            mCompletedCol = (ColumnDefinition)base.GetTemplateChild("mCompletedCol");
            mUnCompletedCol = (ColumnDefinition)base.GetTemplateChild("mUnCompletedCol");
            mTaskBar = (Grid)base.GetTemplateChild("mTaskBar");
        }

        protected override Size MeasureOverride(Size availableSize)
        {
            //20101020 by ChenYao 不知为啥使用百分比出错
            if (availableSize.Width > 0)
            {
                if (mCompletedCol != null && mUnCompletedCol != null)
                {
                    mCompletedCol.Width = new GridLength(availableSize.Width * Data.PercentComplete * 0.01);
                    mUnCompletedCol.Width = new GridLength(1, GridUnitType.Auto);
                }
                if (mTaskBar != null)
                {
                    mTaskBar.Width = availableSize.Width;
                    mTaskBar.Height = availableSize.Height;
                }
            }

            return base.MeasureOverride(availableSize);
        }
    }
}
